package com.kc.road.camera.rabbit;

import com.kc.road.camera.common.Constants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author linkaitao
 * @Date: 2018/2/24.
 * @Desc: rabbitMQ配置
 */
@Configuration
public class RabbitConfig {

    /************** wifi 队列 start ****************/
    @Bean
    public Queue wifiQueue() {
        return new Queue(Constants.WIFI_TOPIC_QUEUE);
    }

    @Bean
    TopicExchange wifiExchange() {
        return new TopicExchange(Constants.WIFI_EXCHANGE);
    }

    @Bean
    Binding bindingExchangeWifi(Queue wifiQueue, TopicExchange wifiExchange) {
        return BindingBuilder.bind(wifiQueue).to(wifiExchange).with(Constants.WIFI_ROUTING_TOPIC_KEY + ".*");
    }

    /************** wifi 队列 end ****************/

    /************** load 队列 start ****************/
    @Bean
    public Queue loadQueue() {
        return new Queue(Constants.LOAD_TOPIC_QUEUE);
    }

    @Bean
    TopicExchange loadExchange() {
        return new TopicExchange(Constants.LOAD_EXCHANGE);
    }

    @Bean
    Binding bindingExchangeLoad(Queue loadQueue, TopicExchange loadExchange) {
        return BindingBuilder.bind(loadQueue).to(loadExchange).with(Constants.LOAD_ROUTING_TOPIC_KEY + ".*");
    }

    /************** load 队列 end ****************/

    /************** scales 队列 start ****************/

    @Bean
    public Queue scalesQueue() {
        return new Queue(Constants.SCALES_TOPIC_QUEUE);
    }

    @Bean
    TopicExchange scalesExchange() {
        return new TopicExchange(Constants.SCALES_EXCHANGE);
    }

    @Bean
    Binding bindingExchangeScales(Queue scalesQueue, TopicExchange scalesExchange) {
        return BindingBuilder.bind(scalesQueue).to(scalesExchange).with(Constants.SCALES_ROUTING_TOPIC_KEY + ".*");
    }

    /************** scales 队列 end ****************/

    /************** camera 队列 start ****************/
    @Bean
    public Queue cameraQueue() {
        return new Queue(Constants.CAMERA_TOPIC_QUEUE, true);
    }

    @Bean
    TopicExchange cameraExchange() {
        return new TopicExchange(Constants.CAMERA_EXCHANGE);
    }

    @Bean
    Binding bindingExchangeCamera(Queue cameraQueue, TopicExchange cameraExchange) {
        return BindingBuilder.bind(cameraQueue).to(cameraExchange).with(Constants.CAMERA_ROUTING_TOPIC_KEY + ".#");
    }
    /************** camera 队列 end ****************/

    /**
     * 消费者数量，默认10
     */
    public static final int DEFAULT_CONCURRENT = 10;

    /**
     * 每个消费者获取最大投递数量 默认50
     */
    public static final int DEFAULT_PREFETCH_COUNT = 10;

    @Bean("pointTaskContainerFactory")
    public SimpleRabbitListenerContainerFactory pointTaskContainerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setPrefetchCount(DEFAULT_PREFETCH_COUNT);
        factory.setConcurrentConsumers(DEFAULT_CONCURRENT);
        configurer.configure(factory, connectionFactory);
        return factory;
    }

}
